<?php

define('IN_DOUCO', true);

require (dirname(__FILE__) . '/include/init.php');

// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'default';

// 图片上传
/*
include_once (ROOT_PATH . 'include/upload.class.php');
$images_dir = 'images/product_category/'; // 文件上传路径，结尾加斜杠
$thumb_dir = ''; // 缩略图路径（相对于$images_dir） 结尾加斜杠，留空则跟$images_dir相同
$img = new Upload(ROOT_PATH . $images_dir, $thumb_dir); // 实例化类文件
if (!file_exists(ROOT_PATH . $images_dir)) mkdir(ROOT_PATH . $images_dir, 0777);
*/
// 赋值给模板
$smarty->assign('rec', $rec);
$smarty->assign('cur', 'product_category');

/**
 * +----------------------------------------------------------
 * 分类列表
 * +----------------------------------------------------------
 */
if ($rec == 'default') {
    $smarty->assign('ur_here', $_LANG['product_category']);
    $smarty->assign('action_link', array (
            'text' => $_LANG['product_category_add'],
            'href' => 'product_category.php?rec=add' 
    ));
    // 赋值给模板
    $smarty->assign('product_category', $dou->get_category_nolevel('product_category'));
    $smarty->display('product_category.htm');
} 

/**
 * +----------------------------------------------------------
 * 分类添加
 * +----------------------------------------------------------
 */
elseif ($rec == 'add') {
    $smarty->assign('ur_here', $_LANG['product_category_add']);
    $smarty->assign('action_link', array (
            'text' => $_LANG['product_category'],
            'href' => 'product_category.php' 
    ));
    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());
    // 赋值给模板
    $smarty->assign('form_action', 'insert');
    $smarty->assign('product_category', $dou->get_category_nolevel('product_category'));
    $smarty->display('product_category.htm');
} 
elseif ($rec == 'insert') {
    if (empty($_POST['cat_name']))
        $dou->dou_msg($_LANG['product_category_name'] . $_LANG['is_empty']);
    if (!$check->is_unique_id($_POST['unique_id']))
        $dou->dou_msg($_LANG['unique_id_wrong']);
    if ($dou->value_exist('product_category', 'unique_id', $_POST['unique_id']))
        $dou->dou_msg($_LANG['unique_id_existed']);
    // 背景图上传
    $image = upload_image('image','product_category');
    //商品描述图
    $image_desc = upload_image('image_desc','product_category');
    //相册图片上传
    $gallery = upload_multi_image('gallery','product_category');
    foreach ($gallery as $k=>$v) {
        $image_ads[$k]['img'] = empty($v) ? '' : $v;
        $image_ads[$k]['link'] = $_POST['link'][$k];
    }
    $image_ads = serialize($image_ads);
    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);
    $price_user_type = json_encode($_POST['user_type_price']);
    $sql = "INSERT INTO " . $dou->table('product_category') . " (cat_id, unique_id, parent_id, cat_name, price, price_user_type, image, image_desc, image_ads, keywords, description, sort)" . " VALUES (NULL, '$_POST[unique_id]', '$_POST[parent_id]', '$_POST[cat_name]', '$_POST[price]', '$price_user_type', '$image', '$image_desc', '$image_ads', '$_POST[keywords]', '$_POST[description]', '$_POST[sort]')";
    $dou->query($sql);
    $dou->create_admin_log($_LANG['product_category_add'] . ': ' . $_POST[cat_name]);
    $dou->dou_header('product_category.php');
} 

/**
 * +----------------------------------------------------------
 * 分类编辑
 * +----------------------------------------------------------
 */
elseif ($rec == 'edit') {
    $smarty->assign('ur_here', $_LANG['product_category_edit']);
    $smarty->assign('action_link', array (
            'text' => $_LANG['product_category'],
            'href' => 'product_category.php' 
    ));
    // 获取分类信息
    $cat_id = $check->is_number($_REQUEST['cat_id']) ? $_REQUEST['cat_id'] : '';
    $query = $dou->select($dou->table('product_category'), '*', '`cat_id` = \'' . $cat_id . '\'');
    $cat_info = $dou->fetch_array($query);
    $price_user_type = json_decode($cat_info['price_user_type']);
    // CSRF防御令牌生成
    $smarty->assign('token', $firewall->get_token());
    // 赋值给模板
    $smarty->assign('form_action', 'update');
    $smarty->assign('product_category', $dou->get_category_nolevel('product_category', '0', '0', $cat_id));
    $smarty->assign('cat_info', $cat_info);
    $smarty->assign('price_user_type', $price_user_type);
    $smarty->assign('image_ads', unserialize($cat_info['image_ads']));
    $smarty->display('product_category.htm');
} 
elseif ($rec == 'update') {
    if (empty($_POST['cat_name']))
        $dou->dou_msg($_LANG['product_category_name'] . $_LANG['is_empty']);
    if (!$check->is_unique_id($_POST['unique_id']))
        $dou->dou_msg($_LANG['unique_id_wrong']);
    if ($dou->value_exist('product_category', 'unique_id', $_POST['unique_id'], "AND cat_id != '$_POST[cat_id]'"))
        $dou->dou_msg($_LANG['unique_id_existed']);
    // 背景图上传
    $image = upload_image('image','product_category');
    if($image) $update_sql .= " image='$image', "; 
    //商品描述图
    $image_desc = upload_image('image_desc','product_category');
    if($image_desc) $update_sql .= " image_desc='$image_desc', ";
    //相册图片上传
    $image_ads = $dou->get_one("SELECT image_ads FROM " . $dou->table('product_category') . " WHERE cat_id = '$_POST[cat_id]'");
    $image_ads = unserialize($image_ads);
    $gallery = upload_multi_image('gallery','product_category');
    foreach ($gallery as $k=>$v) {
        $image_ads[$k]['img'] = empty($v) ? $image_ads[$k]['img'] : $v;
        $image_ads[$k]['link'] = $_POST['link'][$k];
    }
    $image_ads = serialize($image_ads);
    // CSRF防御令牌验证
    $firewall->check_token($_POST['token']);
    $price_user_type = json_encode($_POST['user_type_price']);
    $sql = "UPDATE " . $dou->table('product_category') . " SET cat_name = '$_POST[cat_name]', price = '$_POST[price]', price_user_type='$price_user_type', image_ads='$image_ads', $update_sql  unique_id = '$_POST[unique_id]', parent_id = '$_POST[parent_id]', keywords = '$_POST[keywords]', description = '$_POST[description]', sort = '$_POST[sort]' WHERE cat_id = '$_POST[cat_id]'";
    $dou->query($sql);
    $dou->create_admin_log($_LANG['product_category_edit'] . ': ' . $_POST['cat_name']);
    //$dou->dou_msg($_LANG['product_category_edit_succes'], 'product_category.php');
    $dou->dou_header('product_category.php');
} 

/**
 * +----------------------------------------------------------
 * 分类删除
 * +----------------------------------------------------------
 */
elseif ($rec == 'del') {
    $cat_id = $check->is_number($_REQUEST['cat_id']) ? $_REQUEST['cat_id'] : $dou->dou_msg($_LANG['illegal'], 'product_category.php');
    $cat_name = $dou->get_one("SELECT cat_name FROM " . $dou->table('product_category') . " WHERE cat_id = '$cat_id'");
    $is_parent = $dou->get_one("SELECT id FROM " . $dou->table('product') . " WHERE cat_id = '$cat_id'") .
             $dou->get_one("SELECT cat_id FROM " . $dou->table('product_category') . " WHERE parent_id = '$cat_id'");
    
    if ($is_parent) {
        $_LANG['product_category_del_is_parent'] = preg_replace('/d%/Ums', $cat_name, $_LANG['product_category_del_is_parent']);
        $dou->dou_msg($_LANG['product_category_del_is_parent'], 'product_category.php', '', '3');
    } else {
        if (isset($_POST['confirm']) ? $_POST['confirm'] : '') {
            $dou->create_admin_log($_LANG['product_category_del'] . ': ' . $cat_name);
            $dou->delete($dou->table('product_category'), "cat_id = $cat_id", 'product_category.php');
        } else {
            $_LANG['del_check'] = preg_replace('/d%/Ums', $cat_name, $_LANG['del_check']);
            $dou->dou_msg($_LANG['del_check'], 'product_category.php', '', '30', "product_category.php?rec=del&cat_id=$cat_id");
        }
    }
}


?>